<%@page language="C#" Debug="true" validateRequest=false %>
<%@import namespace="System" %>
<%@import namespace="System.Collections.Generic" %>
<%@import namespace="System.Text.RegularExpressions" %>

<script runat="server">

    protected void Page_Load( object sender, System.EventArgs e )
    {
    }
    
    public string RegexMatch( string input, string pattern, string regexOptions )
    {
    		if( string.IsNullOrEmpty(input) || string.IsNullOrEmpty(pattern) ){
    				return "";
    		}
    		if( string.IsNullOrEmpty(regexOptions) ){
    				regexOptions = "";
    		}
    		
    		List<RegexOptions> optionsList = new List<RegexOptions>(); 	
				if( -1<regexOptions.IndexOf("ignoreCase") ){
						optionsList.Add( RegexOptions.IgnoreCase );
				}
    		if( -1<regexOptions.IndexOf("singleline") ){
    				optionsList.Add( RegexOptions.Singleline );
    		}
    		
    		Match m;
    		if( 1 > optionsList.Count ){
    				m = Regex.Match( input,pattern );
    				return m.Value;
    		}
    		
    		RegexOptions options = optionsList[0];
    		for( int i=1,len=optionsList.Count; i<len; i++ ){
    				options = options | optionsList[i];
    		}
    		
    		m = Regex.Match( input,pattern,options );
    		//m = Regex.Match( input,pattern,RegexOptions.IgnoreCase|RegexOptions.Singleline );
    		return m.Value;
    }
    
    //public string Filter
</script>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

    <title> Regex </title>
    <meta http-equiv="Content-Type" Content="text/html;charset=utf-8" />
    <meta name="Version" content="0.0.0.0" />
    <meta name="Copyright" content="ML" />
    <meta name="LastModify" content="2009-05-31" />
    <style type="text/css">
    	table,th,td{border:1px solid #000;}
    </style>
</head>
<body>
<%
string cmd = Request.QueryString["cmd"];
if( "getResult" != cmd ){
%>
<h1>正则表达式测试器</h1>
<form method="post" action="regex.aspx?cmd=getResult"  target="result_frame">
		输入内容:<br />
		<textarea name="input" style="width:90%;height:150px"></textarea><br />
		输入正则:
		<input name="pattern" type="text" style="width:60%"/>
		<input name="regexOptions" type="checkbox" checked="checked" value="singleline" />单行模式<br />
		<input name="regexOptions" type="checkbox" checked="checked" value="ignoreCase" />忽略大小写<br />
		<input type="submit" value="匹配" />
		</form>
		
		<br />
		输出结果:<br />
		<iframe name="result_frame" src="regex.aspx?cmd=getResult" style="width:90%;height:200px;"></iframe>
<%
}
if( "getResult" == cmd ){
		
		string input = Request.Form["input"];
		string pattern = Request.Form["pattern"];
		string options = Request.Form["regexOptions"];
		
		Response.Write("输入参数:<br />");
		//Response.Write( input +"<br />");
		Response.Write( "正则:" +HttpUtility.HtmlEncode(pattern) +"<br />");
		Response.Write( "参数:" +HttpUtility.HtmlEncode(options) +"<br />");
		
		Response.Write("输出结果:<br />");
 		Response.Write( HttpUtility.HtmlEncode(RegexMatch(input,pattern,options)) +"<br />");
}
%>
</body>
</html>





